package cn.northsea.controller;

import cn.northsea.pojo.*;
import cn.northsea.service.PurchaseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/pur")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    @RequestMapping("/findAll")
    public String findAll(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                          @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                          Model model, @RequestParam(required = false, defaultValue = "") String purchaseOrder
            , @RequestParam(required = false, defaultValue = "") String start,
                          @RequestParam(required = false, defaultValue = "") String end) {
        PageHelper.startPage(pageNum, pageSize);
        List<PurchaseVO> saleList = purchaseService.like(purchaseOrder, start, end);
        PageInfo<PurchaseVO> pageInfo = new PageInfo<>(saleList);
        model.addAttribute("orderPage", pageInfo);
        model.addAttribute("purchaseOrder", purchaseOrder);
        model.addAttribute("start", start);
        model.addAttribute("end", end);
        return "orderPurchase";
    }

    @RequestMapping("/statusPur")
    public String statusPur(@RequestParam(value = "purchaseId") Integer id) {
        purchaseService.statusPur(id);
        return "redirect:/pur/findPurchase.do";
    }

    @RequestMapping("/findPurchase")
    public String findPurchase(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                               @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                               Model model, @RequestParam(required = false, defaultValue = "") String purchaseOrder
            , @RequestParam(required = false, defaultValue = "") String start,
                               @RequestParam(required = false, defaultValue = "") String end) {
        PageHelper.startPage(pageNum, pageSize);
        List<PurchaseTwoVO> saleList = purchaseService.findPurchase(purchaseOrder, start, end);
        PageInfo<PurchaseTwoVO> pageInfo = new PageInfo<>(saleList);
        model.addAttribute("purPage", pageInfo);
        model.addAttribute("purchaseOrder", purchaseOrder);
        model.addAttribute("start", start);
        model.addAttribute("end", end);
        return "orderTwoPurchase";
    }

    @RequestMapping("/findSupplier")
    public String findSupplier(Model model) {
        List<Supplier> erpSuppliers = purchaseService.findSupplier();
        List<Material> materials = purchaseService.findMaterials();
        List<User> user = purchaseService.findUser();
        model.addAttribute("erpSuppliers", erpSuppliers);
        model.addAttribute("materials", materials);
        model.addAttribute("user", user);
        return "addPurchase";
    }

    @RequestMapping("/findName")
    @ResponseBody
    public Supplier findName(String supplierName) {
        return purchaseService.findName(supplierName);
    }

    @RequestMapping("/addPurchase")
    public String addPurchase(@RequestParam("supplierId") Integer supplierId
            , @RequestParam("materialId") Integer materialId
            , @RequestParam("quantity") Integer quantity,
                              @RequestParam("userId") Integer userId
    ) {
        purchaseService.addPurchase(supplierId, materialId, quantity, userId);
        return "redirect:/pur/findPurchase.do";
    }

    @RequestMapping("/addCaiGou")
    @ResponseBody
    public String addCaiGou(@RequestParam("purchaseId") Integer purchaseId,
                            @RequestParam("materialId") Integer materialId) {
        return purchaseService.addCaiGou(purchaseId, materialId);
    }

    @RequestMapping("/delPurchase")
    @ResponseBody
    public String delPurchase(@RequestParam("purchaseId") Integer purchaseId) {
        return purchaseService.delPurchase(purchaseId);
    }

    @RequestMapping("/update")
    public String findUpdate(@RequestParam("purchaseId") Integer purchaseId, Model model) {
        ErpSupplyOrder erpSupplyOrder = purchaseService.findpurchaseIdById(purchaseId);
        List<Material> materials = purchaseService.findMaterials();
        List<User> user = purchaseService.findUser();
        List<Supplier> erpSuppliers = purchaseService.findSupplier();
        model.addAttribute("erpSuppliers", erpSuppliers);
        model.addAttribute("materials", materials);
        model.addAttribute("user", user);
        model.addAttribute("erpSupplyOrder", erpSupplyOrder);
        return "updatePurchase";
    }

    @RequestMapping("/updatePurchase")
    public String updatePurchase(
            @RequestParam("purchaseId") Integer purchaseId,
            @RequestParam("supplierId") Integer supplierId
            , @RequestParam("materialId") Integer materialId
            , @RequestParam("quantity") Integer quantity,
                                 @RequestParam("userId") Integer userId) {
             purchaseService.updatePurchase(purchaseId,supplierId,materialId,quantity,userId);
        return "redirect:/pur/findPurchase.do";
    }
}
